package dyyx.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import dyyx.Data;
import dyyx.DataImportDTO;

public abstract class DataUtil {
	
	
	
	

	public static void main(String[] args) throws Exception {
		// account_541,account_850,2019-07-05 20:20:58,2019-07-05,9594,ip_763,device_331

		String str = "account_541,account_850,2019-07-05 20:20:58,2019-07-05,9594,ip_763,device_331";
		Data data = parseData(str);
		String str2 = data.toString();
		System.out.println("str="+str);
		System.out.println("str2="+str2);
		System.out.println(str.equals(str));


	}
	
	public static Data parseData(String str){
		if(StringUtils.isBlank(str)){
			return null;
		}
		String[]arr = StringUtils.split(str,",");
		if(arr==null || arr.length!=7){
			return null;
		}
		
		String account = arr[0];
		String account_recv = arr[1];
		String trade_time = arr[2];
		String trade_date = arr[3];
		int amount = CommUtil.getInt(arr[4], 0);
		
		if(amount<=0){
			return null;
		}

		String ip = arr[5];
		String device = arr[6];
		
		
		
		Data data = new Data();
		
		data.account = account;
		data.account_recv = account_recv;
		data.trade_date = trade_date;
		data.trade_time = trade_time;
		data.amount = amount;
		data.ip = ip;
		data.device = device;
		
		return data;
	}
	
	
	public static String getInsertSql(String table){
		String s = "insert into "+table+"(account,account_recv,trade_time,trade_date,amount,ip,device) values(?,?,?,?,?,?,?)";
	    return s;
	}
	
	
    public  static void inerst(Connection cn,String insertSql, List<Data> list)throws Exception{
    	if(list==null || list.isEmpty()){
    		return;
    	}
    	PreparedStatement pstmt = null;
		pstmt = cn.prepareStatement(insertSql);
		
		for(Data data:list){
			pstmt.setString(1, data.account);
			pstmt.setString(2, data.account_recv);
			pstmt.setString(3, data.trade_time);
			pstmt.setString(4, data.trade_date);
			pstmt.setInt(5, data.amount);
			pstmt.setString(6, data.ip);
			pstmt.setString(7, data.device);

			pstmt.addBatch();

		}
		pstmt.executeBatch();
		
		
	}
	
	
	public  static void importData(DataImportDTO dto)throws Exception{
		
		
	}
	
}
